#include "StdAfx.h"
#include "tray_msg_item_im.h"
#include "tray_msg.h"
#include <utility/common_function.h>
#include <interface/global_data/user_mgr.h>
#include <interface/global_data_wrapper.h>

//#include "tray_msg_show.h"
tray_msg_item_im::tray_msg_item_im(void)
{
	this->im_msgs_ = NULL;
}

tray_msg_item_im::~tray_msg_item_im(void)
{
	vec_tray_im_msg_item::iterator iter = this->im_msgs_->begin();
	for( ; iter != this->im_msgs_->end(); iter++ )
	{
		if( *iter != NULL )
		{
			delete *iter;
		}
	}
	this->im_msgs_->clear();
}

void
tray_msg_item_im::destroy()
{
	vec_tray_im_msg_item::iterator iter = this->im_msgs_->begin();
	for( ; iter != this->im_msgs_->end(); iter++ )
	{
		if( *iter != NULL )
		{
			delete *iter;
		}
	}
	this->im_msgs_->clear();
	delete this;
}

BOOL
tray_msg_item_im::execute_msg(LPCTSTR userid)
{
	vec_tray_im_msg_item::iterator iter = this->im_msgs_->begin();
	for( ; iter != this->im_msgs_->end(); iter++ )
	{
		aot::tt::command_t cmd;
		cmd.type = WM_GLB_TRAY_MGR_TO_IMCHAT_CHAT_MSG;
		cmd.val = (*iter)->dlg_type;
		cmd.user_data[0] = (*iter)->msg_type;
		cmd.to = (void*)userid;
		cmd.user_data_ptr[0] = (void*)(LPCTSTR)(*iter)->msg_key;
		dll_manager::instance()->get_main_obj()->command_to("idll.imchat.dll", &cmd);
	}
	destroy();
	return TRUE;
}

void 
tray_msg_item_im::push_msg(int im_dlg_type, int im_msg_type, LPCTSTR msgkey)
{
	
}

void
tray_msg_item_im::push_msg(void* msg_data)
{
	BOOL isfirst_ = FALSE;
	tray_msg_item *p = (tray_msg_item*)msg_data;
	tray_msg_item *p1 = new tray_msg_item(p->dlg_type, p->msg_type, p->msg_key);
	if( this->im_msgs_ == NULL )
	{
		this->im_msgs_ = new vec_tray_im_msg_item;
		isfirst_ = TRUE;
	}
	this->im_msgs_->push_back(p1);
	if( isfirst_ )
	{
		_get_info();
	}
	if( this->pdlg_tray_msg_show_ )
	{
		if( this->pdlg_tray_msg_show_->GetSafeHwnd() )
		{
			this->pdlg_tray_msg_show_->refresh_display();
		}
	}
}

void
tray_msg_item_im::merge(tray_msg_item_base *p)
{
	vec_tray_im_msg_item* p1 = (vec_tray_im_msg_item*)(p->get_item_data());
	vec_tray_im_msg_item::iterator iter = p1->begin();
	for( ; iter != p1->end(); iter++ )
	{
		this->im_msgs_->push_back(*iter);
	}
	p1->clear();
	p->destroy();
}

void*
tray_msg_item_im::get_item_data()
{
	return (void*)im_msgs_;
}

CString
tray_msg_item_im::_get_list_text()
{
	CString listtext;
	listtext.Format("%s(%s)", this->name_, get_id());
	return listtext;
}

void
tray_msg_item_im::_get_info()
{
	if( this->im_msgs_ == NULL )
	{
		return;
	}
	vec_tray_im_msg_item::iterator iter = this->im_msgs_->begin();
	if( iter == this->im_msgs_->end() )
	{
		return;
	}
	int dlgtype = (*iter)->dlg_type;
	CString path;
	if( dlgtype == aot::e_single_chat )
	{
		_get_nickname();
		CString myid;
		if( !dll_manager::instance()->get_glb_data() )
		{
			return;
		}
		myid = dll_manager::instance()->get_glb_data()->user_mgr_.id().c_str();
		this->listtext_.Format("%s(%s)", this->name_, get_id());
		aot::tt::iobj_auto_ptr<aot::data::iuser_info> user_info;
		bool ret = dll_manager::instance()->get_main_obj()->glb_data_->user_lst_mgr_->get_user_info(get_id(), user_info);
		CString customface;
		int sex = 1;
		if( ret )
		{
			customface = user_info->face_custom->c_str();
			sex = user_info->sex;
			
		}
		get_user_figure_path(get_id(), myid, customface, sex, false, path);

		
	}
	else if( dlgtype == aot::e_tribe_chat )
	{
		aot::tt::iobj_auto_lock<aot::glb::itribe_mgr> lock(dll_manager::instance()->get_main_obj()->glb_data_->tribe_mgr_.get());
		aot::glb::itribe_info* tribe = dll_manager::instance()->get_main_obj()->glb_data_->tribe_mgr_->get_tribe_info(get_id());
		if ( tribe == NULL )
		{
			return;
		}

		CString tribename = tribe->tribe_info_->clan_name->c_str();
		int picid = tribe->tribe_info_->picture;
		path = get_tribe_figure_by_id(picid);
		this->name_ = tribename;
		this->listtext_.Format("%s(%s)", this->name_, get_id());
	}
	else if( dlgtype == aot::e_multi_chat )
	{
		aot::tt::iobj_auto_lock<aot::glb::itribe_mgr> lock(dll_manager::instance()->get_main_obj()->glb_data_->tribe_mgr_.get());
		aot::glb::itribe_info* tribe = dll_manager::instance()->get_main_obj()->glb_data_->tribe_mgr_->get_tribe_info(get_id());
		if ( tribe == NULL )
		{
			return;
		}
		path = get_tribe_figure_by_id(-1);
		CString tribename = tribe->tribe_info_->clan_name->c_str();
		this->name_ = tribename;
		this->listtext_.Format("%s(%s)", this->name_, get_id());
	}
	else if( dlgtype == aot::e_ent_chat )
	{
		CString id = get_id();
		int pos = id.ReverseFind(',');
		CString departid, userid;;
		userid =  dll_manager::instance()->get_main_obj()->glb_data_->user_mgr_.id().c_str();
		if( pos == -1 )
		{
			departid = id;
		}
		else
		{
			departid = id.Left(pos);
		}
		aot::tt::iobj_auto_ptr<aot::glb::iemployee_info> employee_info;
		if( dll_manager::instance()->get_main_obj()->glb_data_->enterprise_->get_member_info(userid, employee_info) )
		{
			aot::tt::iobj_auto_ptr<aot::data::iclan_info> clan_info;
			if( dll_manager::instance()->get_main_obj()->glb_data_->enterprise_->get_dep_info(departid, clan_info) )
			{
				this->listtext_ = clan_info->clan_name->c_str();
			}
		}
		path = get_tribe_figure_by_id(-2);
	}
	set_icon_path(path);
}

void
tray_msg_item_im::_get_nickname()
{
	name_ = get_user_nick_name(dll_manager::instance()->get_main_obj()->glb_data_, get_id());
}

int 
tray_msg_item_im::get_msg_count()
{
	if( this->im_msgs_ )
	{
		return this->im_msgs_->size();
	}
	else
	{
		return 0;
	}
}

void
tray_msg_item_im::display(HWND hwnd, int row)
{
	CString msg_conut;
	msg_conut.Format("(%d)", get_msg_count());
	dsSetDrawItemValueEx(hwnd, row, 0, "textdefault", this->listtext_, FALSE);
	dsSetDrawItemValueEx(hwnd, row, 0, "default.figure", get_icon_path(), FALSE);
	dsSetDrawItemValueEx(hwnd, row, 0, "textcount", msg_conut, FALSE);
}